package com.csun.nusing.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;

/**
 * 沉浸式的主题 ClassName: StatusBarCompat
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-20
 */
public class StatusBarCompat {
	private static final int INVALID_VAL = -1;
	private static final int COLOR_DEFAULT = Color.parseColor("#20000000");

//	@TargetApi(21) 
//	if (Build.VERSION.SDK_INT >= 21) {
//	    View decorView = getWindow().getDecorView();
//	    int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//	            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
//	    decorView.setSystemUiVisibility(option);
//	    getWindow().setStatusBarColor(Color.TRANSPARENT);
//	}
	
	@TargetApi(Build.VERSION_CODES.LOLLIPOP)
	public static void compat(Activity activity, int statusColor) {

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			if (statusColor != INVALID_VAL) {
				activity.getWindow().setStatusBarColor(statusColor);
			}
			return;
		}

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
				&& Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
			int color = COLOR_DEFAULT;
			ViewGroup contentView = activity
					.findViewById(android.R.id.content);
			if (statusColor != INVALID_VAL) {
				color = statusColor;
			}
			View statusBarView = new View(activity);
			ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT,
					getStatusBarHeight(activity));
			statusBarView.setBackgroundColor(color);
			contentView.addView(statusBarView, lp);
		}

	}

	/**
	 * 完成沉浸式
	 * 那么只需要在Activity里面去写上：StatusBarCompat.compat(this);就可以了。
	 * 如果你希望自己设置状态看颜色，那么就用这个方法：
	 * StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color));
	 * @Description: TODO
	 * @param @param activity   
	 * @return void  
	 * @throws
	 * @author fzq
	 * @date 2017-10-20
	 */
	public static void compat(Activity activity) {
		compat(activity, INVALID_VAL);
	}

	public static int getStatusBarHeight(Context context) {
		int result = 0;
		int resourceId = context.getResources().getIdentifier(
				"status_bar_height", "dimen", "android");
		if (resourceId > 0) {
			result = context.getResources().getDimensionPixelSize(resourceId);
		}
		return result;
	}
}